require(['highcharts','reportCommon'], function () {
	var lineId,dateType="02",enerType="oil",lineName,titleStr="",
	//总能耗、百公里能耗
	totalEnerOil,totalEnerGas,totalEnerEle,
	kmEnerOil,kmEnerGas,kmEnerEle,
	//能耗趋势
	tmpDate=[],lineOilData=[],lineEleData=[],lineGasData=[],
	//百公里能耗排名
	lineEnerOil=[],lineEnerEle=[],lineEnerGas=[],licPltNoArr=[],lineEnerOilStr,lineEnerEleStr,lineEnerGasStr,
	//排名
	trHtml,trHtmlMile;

	function lineChange(event, treeId, treeNode) {
		if (treeNode.type === 'LINE') {
			lineId = treeNode.id;
			lineName = treeNode.name;
			clear();
			$(".tab-panel3 a")[0].click();
			//标题时间
			titleStr = chartTitle(dateType);
			//总能耗、百公里能耗、
			getLineEnerIdxInfo(lineId, dateType);
			//能耗趋势
			getLineEnerTrendInfo(lineId, dateType);
			//线路里程排名 线路百公里能耗
			getLineEnerRankInfo(lineId, dateType);
		} else {
			$.messager.alert("提示", "请选择线路进行查看。");
		}
	}
	//左侧树
	commonFun.leftTreeNodeClickObj['lineAnaEnergyInfo'] = lineChange;
	
	//清空历史记录
	function clear(){
		$("p").html("");
		$("span").html("");
		//总能耗、百公里能耗
		totalEnerOil="",totalEnerGas="",totalEnerEle="",kmEnerOil="",kmEnerGas="",kmEnerEle="",
		//能耗趋势
		tmpDate=[],lineOilData=[],lineEleData=[],lineGasData=[],
		//百公里能耗排名
		lineEnerOil=[],lineEnerEle=[],lineEnerGas=[],licPltNoArr=[],lineEnerOilStr="",lineEnerEleStr="",lineEnerGasStr="";
	}
	
	//顶部日期类型
	$(".time-select a").click(function(){
		if(lineId){
	        dateType = $(this).attr('val');
	        //标题时间
			titleStr = chartTitle(dateType);
	        $(this).addClass("active");
	        $(this).siblings().removeClass("active");
	        //总能耗、百公里能耗、
			getLineEnerIdxInfo(lineId,dateType);
			//能耗趋势
			getLineEnerTrendInfo(lineId,dateType);
			//线路里程排名 线路百公里能耗
			getLineEnerRankInfo(lineId,dateType);
		}
    });
	
	//切换油、电、气
	$(".tab-panel3 a").click(function(){
		enerType = $(this).attr('val');
        $(this).addClass("active");
        $(this).siblings().removeClass("active");
        if(enerType=="oil"){
        	$("#totalEnerTmp").html(totalEnerOil!=undefined?totalEnerOil+"<em>L</em>":"暂无数据");
        	$("#hundKmEnerTmp").html(kmEnerOil!=undefined?kmEnerOil+"<em>L</em>":"暂无数据");
        	lineEnerOilStr=undefined?lineEnerOilStr:"线路百公里油耗趋势";
        	rankHukEner(licPltNoArr,lineEnerOil,lineEnerOilStr);
        }else if(enerType=="ele"){
    		$("#totalEnerTmp").html(totalEnerEle!=undefined?totalEnerEle+"<em>KW/H</em>":"");
    		$("#hundKmEnerTmp").html(kmEnerEle!=undefined?kmEnerEle+"<em>KW/H</em>":"");
    		lineEnerEleStr=undefined?lineEnerEleStr:"线路百公里电耗趋势";
    		rankHukEner(licPltNoArr,lineEnerEle,lineEnerEleStr);
        }else if(enerType=="gas"){
        	 $("#totalEnerTmp").html(totalEnerGas=undefined?totalEnerGas+"<em>L</em>":"");
        	 $("#hundKmEnerTmp").html(kmEnerGas=undefined?kmEnerGas+"<em>L</em>":"" );
        	 lineEnerGasStr=undefined?lineEnerGasStr:"线路百公里气耗趋势";
        	 rankHukEner(licPltNoArr,lineEnerGas,lineEnerGasStr);
        }
    });
	
	//总能耗、百公里能耗、
    function getLineEnerIdxInfo(lineId,dateType){
    	$("p").html("");
    	$.ajaxExt({
			url :"/lineAna/getLineEnerIdxInfo.htm",
			data : {'lineId' : lineId,'dateType' : dateType}
		}).done(function(data){
			$(data).each(function(i,item) {
				if(item.idxType=="050101"){
					totalEnerOil=item.idxValue;
					$("#totalEnerTmp").html(totalEnerOil +"L");
				}else if(item.idxType=="050102"){
					totalEnerEle=item.idxValue;
				}else if(item.idxType=="050103"){
					totalEnerGas=item.idxValue;
				}else if(item.idxType=="050104"){
					kmEnerOil=item.idxValue;
					$("#hundKmEnerTmp").html(kmEnerOil +"L");
				}else if(item.idxType=="050105"){
					kmEnerEle=item.idxValue;
				}else if(item.idxType=="050106"){
					kmEnerGas=item.idxValue;	
				}
			});
		});
    }
    
    //能耗趋势
    function getLineEnerTrendInfo(lineId,dateType){
    	tmpDate=[],lineOilData=[],lineEleData=[],lineGasData=[];
		$.ajaxExt({
			url :"/lineAna/getLineEnerTrendInfo.htm",
			data : {'lineId' : lineId,'dateType' : dateType}
		}).done(function(data){
			$(data).each(function(i,item) {
				xAxisDateFormat(dateType,tmpDate,item.REPORT_DATE );
				if(item.IDX_TYPE=="050201"){
					if(item.TREND_VAL1=="线路百公里油耗趋势"){
						lineOilData.push(parseFloat(item.TREND_VAL2)||0);
					}else if(item.TREND_VAL1=="线路百公里电耗趋势"){
						lineEleData.push(parseFloat(item.TREND_VAL2)||0);
					}else if(item.TREND_VAL1=="线路百公里气耗趋势"){
						lineGasData.push(parseFloat(item.TREND_VAL2)||0);
					}
				}
			});
		//曲线图
	    lineEnerCharts($.unique(tmpDate),lineOilData,lineEleData,lineGasData);
		});
    }
    
    //线路里程排名 线路百公里能耗
    function getLineEnerRankInfo(lineId,dateType){
    	//线路里程排名 
		$.ajaxExt({
			url :"/lineAna/getLineEnerRankInfo.htm",
			data : {'lineId' : lineId,'dateType' : dateType}
		}).done(function(data){
			//排名
			trHtmlMile="";
			$(data).each(function(i,item) {
				if(item.IDX_TYPE=="050301"){
					//里程排名
					trHtmlMile+="<tr><td><span class='rank-tip'>"+ item.RNO +"</span></td><td>"+ item.LIC_PLT_NO +"</td><td>"+ item.RANK_VAL2 +"KM</td></tr>";
				}
			});
			writeTrHtml(trHtmlMile);
		});
		//线路百公里能耗
		$.ajaxExt({
			url :"/lineAna/getLineEnerHukRankInfo.htm",
			data : {'lineId' : lineId,'dateType' : dateType}
		}).done(function(data){
			//排名
			lineEnerOil=[],lineEnerEle=[],lineEnerGas=[],licPltNoArr=[];
			$(data).each(function(i,item) {
				if(item.IDX_TYPE=="050401"){
					licPltNoArr.push(item.LIC_PLT_NO);
					//百公里能耗排名 油
					lineEnerOilStr="线路百公里油耗趋势";
					lineEnerOil.push(parseFloat(item.RANK_VAL2)||0);
				}else if(item.IDX_TYPE=="050402"){
					licPltNoArr.push(item.LIC_PLT_NO);
					//百公里能耗排名 电
					lineEnerEleStr="线路百公里电耗趋势";
					lineEnerEle.push(parseFloat(item.RANK_VAL2)||0);
				}else if(item.IDX_TYPE=="050403"){
					licPltNoArr.push(item.LIC_PLT_NO);
					//百公里能耗排名  气
					lineEnerGasStr="线路百公里气耗趋势";
					lineEnerGas.push(parseFloat(item.RANK_VAL2)||0);
				}
			});
			rankHukEner(licPltNoArr,lineEnerOil,lineEnerOilStr);
		});
    }
    
    function rankHukEner(licPltNoArr,lineEner,lineEnerStr){
    	if(lineEner==""){
    		lineEnerStr="暂无数据";
    	}
    	//柱状图
		$('#lineHukTmpTrend').highcharts({
			chart: {                                                           
	            type: 'bar'                                                    
	        },                
	        title: {
	        	 text: "线路："+lineName +' '+titleStr,
	        	 style:
	             	{
	             	  fontFamily: 'sans-serif',
	         		  fontSize: '14px'
	             	}
	        },
	        credits: {
		          enabled:false
		    },
	        xAxis: {                                                           
	            categories: licPltNoArr
	        },                                                                 
	        yAxis: {                                                           
	            min: 0,         
	            title: {
	                text: ''
	            }
	        },                                                                 
	        legend: {
	            backgroundColor: '#FFFFFF',
	            reversed: true
	        },
	        tooltip: {                                                         
	            valueSuffix: 'L'                                       
	        },   
	        plotOptions: {
	            series: {
	                stacking: 'normal'
	            }
	        },                                                       
	        series: [{              
        	    name: lineEnerStr,
	            data: lineEner                                   
	        }]                                                                 
	    });   
    }
    //曲线图
    function lineEnerCharts(tmpDate,lineOilData,lineEleData,lineGasData){
    	var tickInter;
		if(dateType=="04" || dateType=="05"){
			tickInter=2;
		}else{
			tickInter=1;
		}
    	$('#lineEnerCharts').highcharts({
	        chart: {
	            type: 'spline'
	        },
	        title: {
	            text: "线路："+lineName +' '+titleStr,
	            style:
             	{
             	  fontFamily: 'sans-serif',
         		  fontSize: '14px'
             	}
	        },
	        subtitle: {
	            text: ''
	        },
	        xAxis: {
	        	tickInterval: tickInter,
	            categories: tmpDate
	        },
	        yAxis: {
	            title: {
	                text: 'L'
	            }
	        },
	        credits: {
		          enabled:false
		    },
		    tooltip: {	 
	        	crosshairs: true,
	            shared: true
	        },
	        plotOptions: {
	            line: {
	                dataLabels: {
	                    enabled: true
	                },
	                enableMouseTracking: false
	            }
	        },
	        series: [{
	            name: "线路百公里油耗趋势",
	            data: lineOilData
	        }, {
	            name: "线路百公里电耗趋势",
	            data: lineEleData
	        }, {
	            name: "线路百公里气耗趋势",
	            data: lineGasData
	        }]
	    });
    }
    
    //排名
    function writeTrHtml(trHtmlFlag){
    	trHtml="<tr><th width='15%'>排名</th><th width='40%'>车牌号</th><th>里程</th></tr>";
    	trHtml+=trHtmlFlag;
    	$("#trHtml").html(trHtml);
    }
    
   //如果左侧树有选择车辆则直接查询该车辆数据
	business.execFnByLeftTreeSelectNode('LINE', lineChange);
});